package com.nianzhen.utils;


import android.content.Context;
import android.util.Log;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

public class AsyncAPI {
	public static final int METHOD_GET = 0;
	public static final int METHOD_POST = 1;
	public static final int METHOD_PUT = 2;
	public static final int METHOD_POSTWITHHEADER = 3;

	private static String REST_API_PREFIX = DateConfig.TEST_URL;

	public static final void setRestPrefix(final String addr) {
		REST_API_PREFIX = addr;
	}

	public static RequestHandle post(String url, RequestParams params,JsonHttpResponseHandler responseHandler) {
		return callAPI(DateLib.getAppContext(), url, METHOD_POST, params, responseHandler);
	}

	public static RequestHandle get(String url, RequestParams params, JsonHttpResponseHandler responseHandler) {
		return callAPI(DateLib.getAppContext(), url, METHOD_GET, params, responseHandler);
	}

	private static RequestHandle callAPI(Context context, String url,int method, RequestParams params, JsonHttpResponseHandler responseHandler) {

		if (!NetWorkHelper.isNetworkAvailable(context)) {
			ToastBreak.showToast("亲,网络不给力啊...");
			Log.e("[AsyncAPI]", "network connection doesn't exist");
			return null;
		}
		switch (method) {
		case METHOD_GET:
			return CHSHAsyncHttpClient.get(context, REST_API_PREFIX+url, params,responseHandler);
		case METHOD_POST:
			return CHSHAsyncHttpClient.post(context, REST_API_PREFIX+url, params,responseHandler);
		case METHOD_PUT:
		default:
			return null;
		}
	}

	public static String getREST_API_PREFIX() {
		return REST_API_PREFIX;
	}
}
